home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / lib / mntlib44.zoo / mntlib / readdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-01  |  1.5 KB  |  75 lines

  1. /* readdir routine */
  2.  
  3. /* under MiNT (v0.9 or better) these use the appropriate system calls.
  4.  * under TOS or older versions of MiNT, they use Fsfirst/Fsnext
  5.  *
  6.  * Written by Eric R. Smith and placed in the public domain
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <types.h>
  12. #include <limits.h>
  13. #include <dirent.h>
  14. #include <errno.h>
  15. #include <osbind.h>
  16. #include <mintbind.h>
  17. #include "lib.h"
  18.  
  19. extern int __mint;
  20. extern ino_t __inode;    /* in stat.c */
  21.  
  22. struct dirent *
  23. readdir(d)
  24.     DIR *d;
  25. {
  26.     struct dbuf {
  27.         long ino;
  28.         char name[NAME_MAX + 1];
  29.     } dbuf;
  30.     long r;
  31.     _DTA *olddta;
  32.     struct dirent *dd = &d->buf;
  33.  
  34.     if (__mint > 8) {
  35.         r = (int)Dreaddir((int)(NAME_MAX+1+sizeof(long)), d->handle, (char *) &dbuf);
  36.         if (r == -ENMFIL)
  37.             return 0;
  38.         else if (r) {
  39.             errno = (int) -r;
  40.             return 0;
  41.         }
  42.         dd->d_ino = dbuf.ino;
  43.         dd->d_off++;
  44.         dd->d_reclen = (short)strlen(dbuf.name);
  45.         strcpy(dd->d_name, dbuf.name);
  46.         return dd;
  47.     }
  48. /* ordinary TOS search, using Fsnext. Note that the first time through,
  49.  * Fsfirst has already provided valid data for us; for subsequent
  50.  * searches, we need Fsnext.
  51.  */
  52.     if (d->status == _NMFILE)
  53.         return 0;
  54.     if (d->status == _STARTSEARCH) {
  55.         d->status = _INSEARCH;
  56.     } else {
  57.         olddta = Fgetdta();
  58.         Fsetdta(&(d->dta));
  59.         r = Fsnext();
  60.         Fsetdta(olddta);
  61.         if (r == -ENMFIL) {
  62.             d->status = _NMFILE;
  63.             return 0;
  64.         } else if (r) {
  65.             errno = (int)-r;
  66.             return 0;
  67.         }
  68.     }
  69.     dd->d_ino = __inode++;
  70.     dd->d_off++;
  71.     _dos2unx(d->dta.dta_name, dd->d_name);
  72.     dd->d_reclen = (short)strlen(dd->d_name);
  73.     return dd;
  74. }
  75.